#!/bin/bash

set -e

####
## Functions
####

function disable_whoopsie()
{
    # This functions removes an error

    local conf_file='/usr/share/dbus-1/system.d/org.freedesktop.NetworkManager.conf'
    local patron_start='<policy user="whoopsie">'
    local patron_end='</policy>'

    # Check if the file exists
    if [ ! -f "$conf_file" ]; then return; fi

    # Check if the user exists
    if grep -q 'whoopsie' /etc/passwd; then return; fi

    # Check if the patron matches
    if ! grep -q "$patron_start" "$conf_file"; then return; fi

    # Remove the policy and notify to the service if zenbuntu-desktop is not installed
    # otherwise, the change will be affected as soon as the system restart the dbus by itself
    sudo sed -i "\#$patron_start#,\#$patron_end#d" "$conf_file"
    if [[ ! -d '/usr/share/zenbuntu-desktop' ]]; then
        systemctl restart dbus
    fi
}

function enable_mod() {
    # This function runs the initial configuration the module needs once it is installed
    disable_whoopsie
}


####
## Calls
####

enable_mod

exit 0
